﻿using System;
using System.Collections.Generic;

namespace SR.ExpressionEvaluator.Lib.Core.Functions
{
    internal class Sum : IFunction
    {
        #region Private Members

        private List<object> _Args = new List<object>();

        #endregion

        public List<object> Args
        {
            get
            {
                return _Args;
            }
            set
            {
                _Args = value;
            }
        }

        public object Evaluate()
        {
            // No need to validate as a failed validation won't really affect the evaluation of this function

            double val = 0;
            foreach (object arg in Args)
            {
                try
                {
                    val += Convert.ToDouble(arg);
                }
                catch (Exception ex)
                {
                    throw new Exception("SUM: " + ex.Message);
                }
            }

            return val;
        }

        public bool Validate()
        {
            if (Args.Count == 0)
            {
                return false;
            }
            return true;
        }

        public string GetDefinition()
        {
            return @"SUM(<var1>,<var2>,...)";
        }
    }
}
